#!/usr/bin/env python
"""
# -*- coding: utf-8 -*-
# @Time    : 2020/10/8 12:04
# @Author  : AshJo
# @Desc    : 异步阻塞
# @Filename: async_blocking.py
# @Version : v1.0
"""

# 生产者消费者模型

import requests
from multiprocessing import Process, Queue

url_dic = {
    'cnblog': 'https://www.cnblogs.com/ashjo009/p/13780975.html',
    'douban': 'https://www.douban.com/doulist/38048884/',
    'baidu': 'https://fanyi.baidu.com/#zh/en/%E5%BC%82%E6%AD%A5',
    'gitee': 'https://gitee.com/licq_workspace/hongmeng?_from=gitee_search'
}


def producer(name, url, q):
    ret = requests.get(url)
    q.put((name, ret.text))


def consumer(q):
    while True:
        tup = q.get()
        if tup is None:
            break
        with open(f'{tup[0]}.html', encoding='utf-8', mode='w') as f:
            f.write(tup[1])


if __name__ == '__main__':
    q = Queue()
    p1 = []
    for key in url_dic:
        p = Process(target=producer, args=(key, url_dic[key], q))
        p.start()
        p1.append(p)
    Process(target=consumer, args=(q,)).start()
    for p in p1:
        p.join()
    q.put(None)
